package com.cd.system.service.impl;

import com.cd.common.constant.Constants;
import com.cd.common.domain.SysUser;
import com.cd.system.service.UserService;
import com.cd.common.util.AuthUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Description some init function
 * @Author lqq
 * @Date 2023/11/21 16:26
 **/
@Component
public class ApplicationRunnerImpl implements ApplicationRunner {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(ApplicationRunnerImpl.class);

    @Autowired
    UserService userService;

    @Value("${lqq.dashboard}")
    private String dashboard;

    @Value("${lqq.forceAdmin}")
    private boolean forceAdmin;

    //如果需要注入静态属性
//    @Value("${lqq.dashboard}")
//    private String preUsername;
//    private static String username;
//
//    /**
//     * 在应用程序启动后自动执行的初始化方法
//     */
//    @PostConstruct
//    public void init() {
//        username = preUsername;
//    }


    /**
     * 项目启动时执行
     *
     * @param args
     * @throws Exception
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (dashboard.equals("true")) {
            System.out.printf("\n" +
                    " _                                                          \n" +
                    "| |                                       _                 \n" +
                    "| |   ____   ____     ___  _   _   ___  _| |_  _____  ____  \n" +
                    "| |  / _  | / _  |   /___)| | | | /___)(_   _)| ___ ||    \\ \n" +
                    "| | | |_| || |_| |  |___ || |_| ||___ |  | |_ | ____|| | | |\n" +
                    " \\_) \\__  | \\__  |  (___/  \\__  |(___/    \\__)|_____)|_|_|_|\n" +
                    "        |_|    |_|        (____/                            \n" +
                    "*********************琉球拳系统启动成功!***************\n");

        }
        if (userService.findByUsername(Constants.SUPER_ADMIN) == null) {
        //如果没有管理员账号 强制创建一个
            userService.register(createAdmin(Constants.SUPER_ADMIN));
            userService.register(createAdmin("admin1"));
            logger.info("初始化管理员账号成功");
            return;
        }
        logger.info("lqq已经存在");
    }

    private SysUser createAdmin(String username){

        SysUser user=new SysUser();
//        if (username.equals(Constants.SUPER_ADMIN)){
//        }
        //user.setUserId(1L); 如果需要用admin进行强管控再打开，如admin被恶意篡改
        user.setDeptId(103L);
        user.setUserName(username);
        user.setCreateBy(Constants.SUPER_ADMIN);
        user.setPassword(AuthUtils.encryptPassword(username));
        user.setStatus("0");
        user.setCreateTime(new Date());
        user.setNickName("管理员");
        user.setEmail("lqq@lqq.com");
        user.setPhonenumber(Math.random()*1000000000 +"6");
        user.setDelFlag("0");
        user.setAvatar("");
        user.setLoginIp("127.0.0.1");
        user.setLoginDate(new Date());
        user.setRemark("管理员");
        return user;

    }
}
